package cn.lhl.converter;

import com.opensymphony.xwork2.LocaleProvider;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Locale;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/25/025.
 */
public class Convert {

    public Object convertValue(Object value, Class toType) {
        Object result = null;
        if(value != null) {
            if(value.getClass().isArray() && toType.isArray()) {
                Class componentType = toType.getComponentType();
                result = Array.newInstance(componentType, Array.getLength(value));
                int i = 0;

                for(int icount = Array.getLength(value); i < icount; ++i) {
                    Array.set(result, i, this.convertValue(Array.get(value, i), componentType));
                }
            } else {
                if(toType == Integer.class || toType == Integer.TYPE) {
                    result = Integer.valueOf((int)longValue(value));
                }

                if(toType == Double.class || toType == Double.TYPE) {
                    result = Double.valueOf(doubleValue(value));
                }

                if(toType == Boolean.class || toType == Boolean.TYPE) {
                    result = booleanValue(value)?Boolean.TRUE:Boolean.FALSE;
                }

                if(toType == Byte.class || toType == Byte.TYPE) {
                    result = Byte.valueOf((byte)((int)longValue(value)));
                }

                if(toType == Character.class || toType == Character.TYPE) {
                    result = Character.valueOf((char)((int)longValue(value)));
                }

                if(toType == Short.class || toType == Short.TYPE) {
                    result = Short.valueOf((short)((int)longValue(value)));
                }

                if(toType == Long.class || toType == Long.TYPE) {
                    result = Long.valueOf(longValue(value));
                }

                if(toType == Float.class || toType == Float.TYPE) {
                    result = new Float(doubleValue(value));
                }

                if(toType == BigInteger.class) {
                    result = bigIntValue(value);
                }

                if(toType == BigDecimal.class) {
                    result = bigDecValue(value);
                }

                if(toType == String.class) {
                    result = stringValue(value);
                }

                if(Enum.class.isAssignableFrom(toType)) {
                    result = this.enumValue(toType, value);
                }
            }
        }

        return result;
    }

    public static boolean booleanValue(Object value) {
        if(value == null) {
            return false;
        } else {
            Class c = value.getClass();
            return c == Boolean.class?((Boolean)value).booleanValue():(c == Character.class?((Character)value).charValue() != 0:(value instanceof Number?((Number)value).doubleValue() != 0.0D:true));
        }
    }

    public Enum<?> enumValue(Class toClass, Object o) {
        Enum result = null;
        if(o == null) {
            result = null;
        } else if(o instanceof String[]) {
            result = Enum.valueOf(toClass, ((String[])((String[])o))[0]);
        } else if(o instanceof String) {
            result = Enum.valueOf(toClass, (String)o);
        }

        return result;
    }

    public static long longValue(Object value) throws NumberFormatException {
        if(value == null) {
            return 0L;
        } else {
            Class c = value.getClass();
            return c.getSuperclass() == Number.class?((Number)value).longValue():(c == Boolean.class?(((Boolean)value).booleanValue()?1L:0L):(c == Character.class?(long)((Character)value).charValue():Long.parseLong(stringValue(value, true))));
        }
    }

    public static double doubleValue(Object value) throws NumberFormatException {
        if(value == null) {
            return 0.0D;
        } else {
            Class c = value.getClass();
            if(c.getSuperclass() == Number.class) {
                return ((Number)value).doubleValue();
            } else if(c == Boolean.class) {
                return ((Boolean)value).booleanValue()?1.0D:0.0D;
            } else if(c == Character.class) {
                return (double)((Character)value).charValue();
            } else {
                String s = stringValue(value, true);
                return s.length() == 0?0.0D:Double.parseDouble(s);
            }
        }
    }

    public static BigInteger bigIntValue(Object value) throws NumberFormatException {
        if(value == null) {
            return BigInteger.valueOf(0L);
        } else {
            Class c = value.getClass();
            return c == BigInteger.class?(BigInteger)value:(c == BigDecimal.class?((BigDecimal)value).toBigInteger():(c.getSuperclass() == Number.class?BigInteger.valueOf(((Number)value).longValue()):(c == Boolean.class?BigInteger.valueOf(((Boolean)value).booleanValue()?1L:0L):(c == Character.class?BigInteger.valueOf((long)((Character)value).charValue()):new BigInteger(stringValue(value, true))))));
        }
    }

    public static BigDecimal bigDecValue(Object value) throws NumberFormatException {
        if(value == null) {
            return BigDecimal.valueOf(0L);
        } else {
            Class c = value.getClass();
            return c == BigDecimal.class?(BigDecimal)value:(c == BigInteger.class?new BigDecimal((BigInteger)value):(c.getSuperclass() == Number.class?new BigDecimal(((Number)value).doubleValue()):(c == Boolean.class?BigDecimal.valueOf(((Boolean)value).booleanValue()?1L:0L):(c == Character.class?BigDecimal.valueOf((long)((Character)value).charValue()):new BigDecimal(stringValue(value, true))))));
        }
    }

    public static String stringValue(Object value, boolean trim) {
        String result;
        if(value == null) {
            result = "null";
        } else {
            result = value.toString();
            if(trim) {
                result = result.trim();
            }
        }

        return result;
    }

    public static String stringValue(Object value) {
        return stringValue(value, false);
    }



}
